<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2007/08/06 05:14:42 $)
// $RCSfile: client_loginhandler.php,v $ : $Revision: 1.18 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

require_once ("./includes/functions_html.php");

/**
* ###############################################
* CLIENT LOGIN
* ###############################################
*/
if ($eventaction == "login")
{
	// Do we need to cookie the username and password?
	$loginemailcookie = $cookie->getSCookie("loginemail");
	$loginpasscookie = $cookie->getSCookie("loginpassword");

	if ($loginemailcookie != $_REQUEST["loginemail"] || $loginpasscookie != $_REQUEST["loginpassword"])
	{
		$cookie->emptySCookie("loginemail");
		$cookie->emptySCookie("loginpassword");
	}

	if ($_POST["rememberme"] == 1)
	{
		$cookie->setSCookie("loginemail", $_POST["loginemail"], true);
		$cookie->setSCookie("loginpassword", md5($_POST["loginpassword"]), true);
	} else {
		$cookie->emptySCookie("loginemail");
		$cookie->emptySCookie("loginpassword");
	}

	if (trim($_REQUEST["loginemail"]) != "" && trim($_REQUEST["loginpassword"]) != "")
	{
		$loginresult = $loginshare->authorize(strtolower($_REQUEST["loginemail"]), $_REQUEST["loginpassword"]);
	} else {
		$loginresult = -5;
	}

	if ($_SWIFT["user"]["usergroupid"] != $_SWIFT["tgroup"]["regusergroupid"] && $_SWIFT["tgroup"]["restrictgroups"] == 1 && $loginresult == 1)
	{
		// Restriction is in place

		$loginshare->logout();
		$tgroup = $_SWIFT["tgroup"]["title"];


		if (!empty($_POST["querystring"]) && !stristr($_POST["querystring"], "&do=va") && !stristr($_POST["querystring"], "loginresult=0"))
		{
			$_redirecturl = "index.php?loginresult=0&group=".urlencode($tgroup)."&".$_POST["querystring"];
		} else {
			$_redirecturl = "index.php?loginresult=0&group=".urlencode($tgroup);
		}

		printRedirect($_SWIFT["language"]["processinglogin"], htmlspecialchars($_redirecturl), true);
		exit;
	}

	// Lookup for a different template group
	$tgroup = $_SWIFT["tgroup"]["title"];

	// Does this user belong to this user group?
	if ($_SWIFT["user"]["usergroupid"] != $_SWIFT["tgroup"]["regusergroupid"])
	{
		foreach ($_SWIFT["tgroupcache"] as $key=>$val)
		{
			if ($val["regusergroupid"] == $_SWIFT["user"]["usergroupid"] && !empty($_SWIFT["user"]["usergroupid"]) && $_SWIFT["tgroup"]["tgroupid"] != $val["tgroupid"])
			{
				$tgroup = $val["title"];
				break;
			}
		}
	}

	// Are we supposed to redirect with parameters?
	if (!empty($_POST["querystring"]) && !stristr($_POST["querystring"], "&do=va") && !stristr($_POST["querystring"], "loginresult=0"))
	{
		$_redirecturl = "index.php?loginresult=".urlencode($loginresult)."&group=".urlencode($tgroup)."&".$_POST["querystring"];
	} else {
		$_redirecturl = "index.php?loginresult=".urlencode($loginresult)."&group=".urlencode($tgroup);
	}

	printRedirect($_SWIFT["language"]["processinglogin"], htmlspecialchars($_redirecturl), true);
	exit;



/**
* ###############################################
* CLIENT LOGOUT
* ###############################################
*/
} else if ($eventaction == "logout") {
	$logoutresult = $loginshare->logout();

	if ($_SWIFT["settings"]["g_returnlogout"] == '1')
	{
		// Reset Template Group
		$cookie->parseCookie("client");
		$cookie->addCookie("client", "groupid", false);
		$cookie->buildCookie("client", true);
	}
	
	printRedirect($_SWIFT["language"]["loggingout"], "index.php?logoutresult=".urlencode($logoutresult), true);
	exit;
}
?>